package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiCatchLogData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiCatchLogDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiCatchLogData> {

    public long getMaxId(int activityId, int groupId) {
        String sql = "select max(id) as maxId from t_c_xing_you_team_catch_log_data where activityId=? and groupId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                long data = rs.getLong("maxId");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }


    public static String getCrossXingYouShanShuiCatchLogDataSql = "select * from `t_c_xing_you_team_catch_log_data` where `activityId`=? and groupId = ?";
    public Map<Integer, List<CrossXingYouShanShuiCatchLogData>> getCrossXingYouShanShuiCatchLogData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossXingYouShanShuiCatchLogData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiCatchLogDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiCatchLogData data = parse(rs);
                if(!result.containsKey(data.getTeamId())){
                    result.put(data.getTeamId(), new ArrayList<>());
                }
                result.get(data.getTeamId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossXingYouShanShuiCatchLogDataSql = "update `t_c_xing_you_team_catch_log_data` set teamId=?,targetTeamId=?,targetUserId=?,seizeCount=?,catchTime=?,nickName=? where activityId=? and groupId = ? and id=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiCatchLogData crossXingYouShanShuiCatchLogData) {
//        crossXingYouShanShuiCatchLogData.setBeginWriteTime();
//        getDb().setConnName(DbNameStrategy.getCrossDb());
//        int i = 1;
//        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getTeamId()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getTargetTeamId()));
//        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiCatchLogData.getTargetUserId()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getSeizeCount()));
//        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiCatchLogData.getCatchTime()));
//        para.put(i++, new DbParameter(Types.VARCHAR, crossXingYouShanShuiCatchLogData.getNickName()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getActivityId()));
//        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getGroupId()));
//        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiCatchLogData.getId()));
//        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiCatchLogDataSql, para) > 0) {
//            return true;
//        }
        return false;
    }

    public static String insertCrossXingYouShanShuiCatchLogDataSql = "insert into `t_c_xing_you_team_catch_log_data`(activityId,groupId,id,teamId,targetTeamId,targetUserId,seizeCount,catchTime,nickName) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiCatchLogData crossXingYouShanShuiCatchLogData) {
        crossXingYouShanShuiCatchLogData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiCatchLogData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getTargetTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiCatchLogData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiCatchLogData.getSeizeCount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiCatchLogData.getCatchTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossXingYouShanShuiCatchLogData.getNickName()));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiCatchLogDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiCatchLogData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiCatchLogData crossXingYouShanShuiCatchLogData = new CrossXingYouShanShuiCatchLogData();
        crossXingYouShanShuiCatchLogData.setActivityId(rs.getInt("activityId"));
        crossXingYouShanShuiCatchLogData.setId(rs.getLong("id"));
        crossXingYouShanShuiCatchLogData.setTeamId(rs.getInt("teamId"));
        crossXingYouShanShuiCatchLogData.setTargetTeamId(rs.getInt("targetTeamId"));
        crossXingYouShanShuiCatchLogData.setTargetUserId(rs.getLong("targetUserId"));
        crossXingYouShanShuiCatchLogData.setSeizeCount(rs.getString("seizeCount"));
        crossXingYouShanShuiCatchLogData.setCatchTime(rs.getLong("catchTime"));
        crossXingYouShanShuiCatchLogData.setNickName(rs.getString("nickName"));
        crossXingYouShanShuiCatchLogData.setGroupId(rs.getInt("groupId"));

        return crossXingYouShanShuiCatchLogData;
    }
}
